package com.h3c.ptability.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenyifan
 * @date 2022/5/20 14:06
 * 电子签章记录文件的状态枚举类
 */
@Getter
@AllArgsConstructor
public enum ElectronicSignRecordInfoStatus {

    /**
     * 该文件签署状态  0签署中
     */
    SIGNING(0,"签署中"),

    /**
     * 1成功
     */
    SIGN_SUCCESS(1,"成功"),
    /**
     * 2异常
     */
    SIGN_FAIL(2,"签署异常"),

    /**
     * 3待公司签章
     */
    WAIT_ORG_SIGN(3,"待公司签章"),

    /**
     * 4 作废
     */
    WASTE_SIGN_PROCESS(4,"作废");

    @EnumValue
    @JsonValue
    private final Integer code;

    private final String statusName;

    /**
     * code 作为 key，封装为 Map
     */
    private static final Map<Integer, ElectronicSignRecordInfoStatus> CODE_MAP = Stream
            .of(ElectronicSignRecordInfoStatus.values())
            .collect(Collectors.toMap(ElectronicSignRecordInfoStatus::getCode, Function.identity()));

    /**
     * 静态工厂反序列化
     *
     * @param key code
     * @return 评论的类型枚举
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static ElectronicSignRecordInfoStatus valueOfKey(Integer key) {
        return Optional.ofNullable(CODE_MAP.get(key))
                .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
    }
}
